#include "protocoleditform.h"
#include "ui_protocoleditform.h"
#include "paramtypedelegate.h"

ProtocolEditForm::ProtocolEditForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ProtocolEditForm)
{
    ui->setupUi(this);

    initParamTable(ui->tableWidget_Params);
    ui->comboBox_RwOptions->addItems(Global::instance()->accessStringList());
}

ProtocolEditForm::~ProtocolEditForm()
{
    delete ui;
}

void ProtocolEditForm::setProcotolItem(struProtocolItem protocolItem)
{
    ui->lineEdit_Name->setText(protocolItem.strName);
    ui->lineEdit_Note->setText(protocolItem.strNote);
    ui->lineEdit_CmdCode->setText("0x" + QString::number(protocolItem.iCode, 16).toUpper());

    ui->checkBox_IsArray->setChecked(protocolItem.bArray);
    ui->spinBox_ArrayCount->setValue(protocolItem.iArrayCount);
    ui->spinBox_ArrayCount->setEnabled(protocolItem.bArray);

    ui->checkBox_IsPrivate->setChecked(protocolItem.bPrivate);
    ui->comboBox_RwOptions->setCurrentText(Global::instance()->accessToString( protocolItem.access));

    // 参数列表
    QTableWidget* table = ui->tableWidget_Params;
    int paramCount = protocolItem.paramList.count();
    table->clearContents();
    table->setRowCount(paramCount);
    for (int i=0;i<paramCount;i++)
    {
        ParamItem param = protocolItem.paramList.at(i);

        table->setItem(i, 0, new QTableWidgetItem(param.strName));
        table->setItem(i, 1, new QTableWidgetItem(Global::instance()->paramTypeToString(param.type)));
        table->setItem(i, 2, new QTableWidgetItem(param.strNote));
    }

}

struProtocolItem ProtocolEditForm::getProtocolItem()
{
    struProtocolItem item;
    item.strName = ui->lineEdit_Name->text();
    item.strNote = ui->lineEdit_Note->text();

    QString strCmdCode = ui->lineEdit_CmdCode->text();
    bool bOk;
    int iCmdCode = strCmdCode.toInt(&bOk, 10);
    if(strCmdCode.indexOf("0x") >= 0)
    {
        iCmdCode = strCmdCode.toInt(&bOk, 16);
    }
    item.iCode = static_cast<quint8>(iCmdCode);

    item.bArray = ui->checkBox_IsArray->isChecked();
    item.iArrayCount = ui->spinBox_ArrayCount->value();
    item.bPrivate = ui->checkBox_IsPrivate->isChecked();
    item.access = Global::instance()->stringToAccess(ui->comboBox_RwOptions->currentText());

    // 参数列表
    QTableWidget* table = ui->tableWidget_Params;
    int paramCount = table->rowCount();
    for(int i=0; i<paramCount; i++)
    {
        ParamItem param;
        param.strName = table->item(i,0)->data(0).toString();
        param.type = Global::instance()->stringToParamType( table->item(i,1)->data(0).toString());
        param.strNote = table->item(i,2)->data(0).toString();

        item.paramList.append(param);
    }

    return item;
}

void ProtocolEditForm::initParamTable(QTableWidget *table)
{
    QStringList headers;
    headers << tr("参数名称") << tr("参数类型") << tr("备注");
    table->setColumnCount(3);
    table->setHorizontalHeaderLabels(headers);
    table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    table->setItemDelegateForColumn(1, new ParamTypeDelegate(this));

    table->setRowCount(0);

}

void ProtocolEditForm::on_pushButton_AddParam_clicked()
{
    QTableWidget* table = ui->tableWidget_Params;
    int n = table->rowCount();
    table->insertRow(n);

    table->setItem(n, 0, new QTableWidgetItem(QString("param%1").arg(n)));
    table->setItem(n, 1, new QTableWidgetItem("int8"));
    table->setItem(n, 2, new QTableWidgetItem(QString("参数%1").arg(n)));
}

void ProtocolEditForm::on_pushButton_RemoveCurrentParam_clicked()
{
    QTableWidget* table = ui->tableWidget_Params;

    int currentRow = table->currentRow();
    if(currentRow < 0)
    {
       return;
    }

    table->removeRow(currentRow);
}


void ProtocolEditForm::on_lineEdit_Name_editingFinished()
{
    QString str = QString("%1(%2)").arg(ui->lineEdit_Note->text()).arg(ui->lineEdit_Name->text());
    emit nameChanged(str);
}

void ProtocolEditForm::on_lineEdit_Note_editingFinished()
{
    QString str = QString("%1(%2)").arg(ui->lineEdit_Note->text()).arg(ui->lineEdit_Name->text());
    emit nameChanged(str);
}

void ProtocolEditForm::on_checkBox_IsArray_stateChanged(int arg1)
{
    ui->spinBox_ArrayCount->setEnabled(ui->checkBox_IsArray->isChecked());
}
